package com.example.youhuo.a_fragment

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.example.mvp.view.BaseFragment
import com.example.myapplication.R
import com.example.myapplication.adapter.Car_Adapter
import com.example.myapplication.antivity.Car_ByFood_Entity
import com.example.myapplication.entity.CarGoods
import com.example.myapplication.z_c_car.Car_ByFood
import com.example.yoho.mvp.entitiy.VersionEntity
import com.example.youhuo.entity.BaseEntity
import com.example.youhuo.mvp.contract.LjlContract
import com.example.youhuo.mvp.presenter.LjlPresenter
import com.example.youhuo.network.Api
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.car_item.*
import kotlinx.android.synthetic.main.fragment_sort__blank.*
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory


class Car_Fragment : BaseFragment<LjlPresenter>(), LjlContract.LjlView {

    private val carRec: RecyclerView by lazy { view1.findViewById<RecyclerView>(R.id.car_rec) }
    private val carCheall: CheckBox by lazy { view1.findViewById<CheckBox>(R.id.car_cheall) }
    private val carSum: TextView by lazy { view1.findViewById<TextView>(R.id.car_sum) }
    private val carOver: Button by lazy { view1.findViewById<Button>(R.id.car_over) }
    private lateinit var adapter: Car_Adapter
    private var sum: Float = 0.0F
    //用来判断是否全部选中
    private var alltype: Int = 0
    //用来减商品
    private var jian:Int = 1
    private var goodslist:ArrayList<Car_ByFood_Entity>?= null

    override fun findlayout(): Int {
        return R.layout.fragment_three_
    }

    override fun initView() {
        goodslist = ArrayList()
        var builder1: Retrofit.Builder = Retrofit.Builder()
        builder1.baseUrl("http://10.161.7.81/")
        builder1.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        builder1.addConverterFactory(GsonConverterFactory.create())
        builder1.client(
            OkHttpClient.Builder().addInterceptor(
                HttpLoggingInterceptor().setLevel(
                    HttpLoggingInterceptor.Level.BODY
                )
            ).build()
        )
        var build = builder1.build()
        build.create(Api::class.java)
            .getCarGoods()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(object : Observer<CarGoods> {
                override fun onComplete() {
                }

                override fun onSubscribe(d: Disposable) {
                }

                override fun onNext(t: CarGoods) {
                    var values = t.values
                    adapter = Car_Adapter(values as ArrayList<CarGoods.cargood>)
                    carRec.adapter = adapter
                    carRec.layoutManager = LinearLayoutManager(context)
                    carSum.text = "总价为:" + sum
                    carCheall.setOnClickListener(View.OnClickListener {
                        if (carCheall.isChecked) {
                            for (i in 0..values.size - 1) {
                                if (values[i].ischecked) {

                                } else {
                                    values[i].ischecked = true
                                    var toFloat = values[i].shop_price?.toFloat()
                                    var toInt = values[i].shop_num?.toInt()
                                    sum += (toFloat!! * toInt!!)
                                    carSum.text = "总价为:" + sum
                                    adapter.notifyDataSetChanged()
                                    alltype = values.size
                                }

                            }
                        } else {
                            for (i in 0..values.size - 1) {

                                values[i].ischecked = false
                                var toFloat = values[i].shop_price?.toFloat()

                                sum = 00.00F
                                carSum.text = "总价为:" + sum
                                adapter.notifyDataSetChanged()
                                alltype = 0

                            }
                        }
                    })

                    adapter.setOnItemChildClickListener(object :
                        BaseQuickAdapter.OnItemChildClickListener {
                        override fun onItemChildClick(
                            adapter: BaseQuickAdapter<*, *>?,
                            view: View?,
                            position: Int
                        ) {
                            when (view?.id) {
                                R.id.car_add -> {
                                    var toInt = values[position].shop_num?.toInt()
                                    if (toInt != null) {
                                        toInt++
                                    }
                                    if (values[position].ischecked){
                                        var toFloat = values[position].shop_price?.toFloat()
                                        sum+= toFloat!!
                                        carSum.setText("总价为:" + sum)
                                    }
                                        values[position].shop_num = "" + toInt
                                    adapter?.notifyDataSetChanged()
                                }
                                R.id.car_delete -> {
                                    var toInt = values[position].shop_num?.toInt()
                                    if (toInt != null && toInt > 1) {
                                        toInt--
                                        if (values[position].ischecked ){

//                                        var toFloat = values[position].shop_price?.toFloat()
//                                        sum-= toFloat!!
//                                        carSum.setText("总价为:" + sum)
                                            var toFloat = values[position].shop_price?.toFloat()
                                            sum -= toFloat!!
                                            carSum.setText("总价为:" + sum)

                                        }
                                    }


                                    values[position].shop_num = "" + toInt
                                    adapter?.notifyDataSetChanged()
                                }
                                R.id.car_food_check -> {

                                    if (values[position].ischecked) {
                                        var toInt = values[position].shop_price?.toFloat()
                                        var num = values[position].shop_num?.toInt()
                                        alltype--
                                        sum -= (toInt!! * num!!)
                                        carSum.setText("总价为:" + sum)
                                        values[position].ischecked = false
                                        adapter?.notifyDataSetChanged()
                                    } else {
                                        var toInt = values[position].shop_price?.toFloat()
                                        var num = values[position].shop_num?.toInt()
                                        sum += (toInt!! * num!!)
                                        carSum.setText("总价为:" + sum)
                                        values[position].ischecked = true
                                        adapter?.notifyLoadMoreToLoading()
                                        alltype++
                                    }
                                    Log.e("ljl", "" + alltype + "------" + values.size)
                                    if (alltype == values.size) {
                                        carCheall.isChecked = true
                                    } else {
                                        carCheall.isChecked = false

                                    }
                                }

                            }


                        }

                    })



                    carOver.setOnClickListener(View.OnClickListener {
                        var intent = Intent(context,Car_ByFood::class.java)
                        for (i in 0 .. values.size-1){
                            if (values[i].ischecked){
                                var price = values[i].shop_price?.toFloat()
                                var num = values[i].shop_num?.toInt()
                                var name = values[i].shop_name

                                goodslist?.add(Car_ByFood_Entity(name,price,num))

                                intent.putExtra("goods",goodslist)

                            }
                        }
                        startActivity(intent)

                    })


                }

                override fun onError(e: Throwable) {
                    Log.e("car", "" + e.message)
                }

            })





    }

    override fun initData() {

    }


    override fun showmsg(entity: BaseEntity) {

    }


}